package com.hyperf.spring.boot.autoconfigure.lock;

import com.hyperf.core.lock.executor.LockExecutor;
import com.hyperf.core.lock.executor.ZookeeperLockExecutor;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lizhaoyang
 */
@Configuration
@ConditionalOnClass(CuratorFramework.class)
@ConditionalOnProperty(prefix = "hyperf.lock", name = "enabled", havingValue = "true", matchIfMissing = true)
public class ZookeeperLockExecutorAutoConfiguration {

    private CuratorFramework curatorFramework;

    public ZookeeperLockExecutorAutoConfiguration(CuratorFramework curatorFramework) {
        this.curatorFramework = curatorFramework;
    }

    @Bean
    @ConditionalOnMissingBean(LockExecutor.class)
    @ConditionalOnProperty(prefix = "hyperf.lock", name = "lock-type", havingValue = "zookeeper")
    public LockExecutor lockExecutor() {
        return new ZookeeperLockExecutor(curatorFramework);
    }
}
